package com.szqhyxj.youxiangju.biz.merchantHome.shop.addSite;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.szqhyxj.youxiangju.BaseActivity;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.DefaultSite;
import com.szqhyxj.youxiangju.sys.components.NavigationBar;
import com.szqhyxj.youxiangju.sys.components.androidpicker.AddressPickTask;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.entity.City;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.entity.County;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.entity.Province;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.util.ConvertUtils;
import com.szqhyxj.youxiangju.sys.components.androidpicker.qqtheme.framework.util.PickerUtil;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.autosize.internal.CustomAdapt;

public class AddSiteActivity extends BaseActivity implements AddSiteContract.View, NavigationBar.NavigationBarListener, CustomAdapt{

    private static final String DEFAULTSITE = "defaultSite";

    @BindView(R.id.navigationBar)
    NavigationBar navigationBar;
    @BindView(R.id.input_name)
    EditText inputName;
    @BindView(R.id.input_phone)
    EditText inputPhone;
    @BindView(R.id.in_the_area)
    TextView inTheArea;
    @BindView(R.id.inphut_detailed_address)
    EditText inphutDetailedAddress;
    @BindView(R.id.present)
    Button present;
    private AddSitePresenter presenter;
    private DefaultSite defaultSite;

    @Override
    public boolean isBaseOnWidth(){
        return true;
    }

    @Override
    public float getSizeInDp(){
        return 360;
    }

    public static abstract class SimpleTextWatcher implements TextWatcher {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }

    public static void start(Activity activity){
        Intent intent = new Intent();
        intent.setClass(activity, AddSiteActivity.class);
        activity.startActivity(intent);
    }


    public static void start(Activity activity,DefaultSite defaultSite){
        Intent intent = new Intent();
        intent.setClass(activity, AddSiteActivity.class);
        intent.putExtra(DEFAULTSITE,defaultSite);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_site);
        ButterKnife.bind(this);
        defaultSite = (DefaultSite) getIntent().getSerializableExtra(DEFAULTSITE);

        navigationBar.setNavigationBarListener(this);
        presenter = new AddSitePresenter(this);

        if(defaultSite !=null){
            presenter.init(defaultSite.getAddrId()+"");
        }
        initEvent();
    }


    public void initEvent(){
        inputName.addTextChangedListener(new SimpleTextWatcher(){
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2){
                presenter.setUsername(charSequence.toString());
            }
        });

        inputPhone.addTextChangedListener(new SimpleTextWatcher(){
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2){
                presenter.setPhone(charSequence.toString());
            }
        });

        inphutDetailedAddress.addTextChangedListener(new SimpleTextWatcher(){
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2){
                presenter.setAddress(charSequence.toString());

            }
        });
    }


    @OnClick({R.id.in_the_area_line, R.id.present})
    public void onViewClicked(View view){
        switch(view.getId()){
            case R.id.in_the_area_line:
                showAddress();
                break;
            case R.id.present:
                if(defaultSite == null){
                    presenter.presentAddress();
                }else {
                    presenter.editDddress();
                }
                break;
        }
    }

    @Override
    public void navigationLeft(){
        finish();
    }

    @Override
    public void navigationRight(){

    }

    @Override
    public void showAddress(){
        String json = null;
        try {
            json = ConvertUtils.toString(getAssets().open("result.json"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        PickerUtil.onAddressPicker(this,json, new AddressPickTask.Callback(){
            @Override
            public void onAddressInitFailed(){

            }

            @Override
            public void onAddressPicked(Province province, City city, County county){
                presenter.setProvinceCode(province.getAreaId());
                presenter.setCityCode(city.getAreaId());
                presenter.setCountyCode(county.getAreaId());

                inTheArea.setText(province.getAreaName()+" "+city.getAreaName()+" "+county.getAreaName());
            }
        });
    }

    @Override
    public void showDefaultSite(DefaultSite defaultSite){
        inputName.setText(defaultSite.getUsername());
        inputPhone.setText(defaultSite.getPhone());
        inphutDetailedAddress.setText(defaultSite.getAddress());
        inTheArea.setText(defaultSite.getFullName());

        presenter.setProvinceCode(defaultSite.getProvinceCode());
        presenter.setCityCode(defaultSite.getCityCode());
        presenter.setCountyCode(defaultSite.getCountyCode());
        presenter.setPhone(inputPhone.getText().toString());
        presenter.setAddress(inphutDetailedAddress.getText().toString());
    }

    @Override
    public void finishThis(){
        finish();
    }
}
